package cn.edu.hrbcu.es.invigilatemanagerment.controller;
import cn.edu.hrbcu.es.invigilatemanagerment.pojo.School;
import cn.edu.hrbcu.es.invigilatemanagerment.pojo.Speciality;
import cn.edu.hrbcu.es.invigilatemanagerment.pojo.University;
import cn.edu.hrbcu.es.invigilatemanagerment.service.ISchoolService;
import cn.edu.hrbcu.es.invigilatemanagerment.service.ISpecialityService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("Speciality")
public class SpecialityController {
    @Autowired
    private ISpecialityService specialityService;
    @Resource
    private ISchoolService schoolService;

    @RequestMapping(value = {"/toAddSpeciality"})
    public String toAddSpeciality(Model model) {
        List<School> schools = schoolService.queryAllSchool();
        model.addAttribute("schools", schools);
        return "toaddspeciality";
    }

    @RequestMapping(value = {"/toUpdateSpeciality"})
    public ModelAndView toUpdateSpeciality(Integer id) {
        //int id = Integer.parseInt(model.getAttribute("id").toString());
        Speciality speciality = specialityService.querySpecialityById(id);
        ModelAndView model = new ModelAndView("toupdatespeciality");

        List<School> schools = schoolService.queryAllSchool();
        model.addObject("schools", schools);
        model.addObject("speciality", speciality);
        return model;
    }

    @ResponseBody
    @RequestMapping("insertSpeciality")
    public String Speciality(String name,String abbreviation,int schoolId,String note)
    {
        Speciality speciality = new Speciality(null,name,abbreviation,schoolId,note);
        specialityService.insertSpeciality(speciality);
        return "OK";
    }

    @RequestMapping("queryAllSpeciality")
    public String queryAllSpeciality(Model model,@RequestParam(value = "pageNow",defaultValue = "1")int pageNow,
                                     @RequestParam(value = "pageSize",defaultValue = "5")int pageSize)
    {
        int totalCount = specialityService.queryTotalSpecialityCount();
        //  总条数/页数再向上取整
        int pageCount = (int) Math.ceil((double) totalCount / pageSize);

        List<Map<String,Object>> specialities = new ArrayList<Map<String,Object>>();
        List<Speciality> Specialities_ = specialityService.querySpecialityByPage(pageNow,pageSize);

        if(Specialities_ != null && Specialities_.size() > 0){
            for (Speciality speciality_ : Specialities_) {
                Map<String,Object> speciality = new HashMap<>();
                School school = schoolService.querySchoolById(speciality_.getSchoolId());
                speciality.put("id",speciality_.getId());
                speciality.put("name",speciality_.getName());
                speciality.put("abbreviation",speciality_.getAbbreviation());
                speciality.put("school",school.getName());
                speciality.put("note",speciality_.getNote());
                specialities.add(speciality);
            }
        }
        model.addAttribute("specialities", specialities);
        model.addAttribute("pageCount",pageCount);
        model.addAttribute("pageNow",pageNow);
        model.addAttribute("pageSize",pageSize);
        return "speciality";
    }



    @ResponseBody
    @GetMapping("querySpecialityById")
    public Speciality querySpecialityById(Integer id)
    {
        return specialityService.querySpecialityById(id);
    }

    @ResponseBody
    @RequestMapping("deleteSpecialityById")
    public String deleteSpecialityById(Integer id)
    {
        specialityService.deleteSpecialityById(id);
        return "OK";
    }

    @ResponseBody
    @RequestMapping("updateSpecialityById")
    public String updateSpecialityById(Integer id,String name,String abbreviation,Integer schoolId,String note)
    {
        Speciality speciality = new Speciality(id,name,abbreviation,schoolId,note);
        specialityService.updateSpecialityById(speciality);
        return "OK";
    }
}
